package com.example.demo.config;

import com.example.demo.dao.PermissionRepository;
import com.example.demo.dao.UserRepository;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

@Configuration

public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserRepository userRepository;
    @Autowired
    PermissionRepository permissionRepository;
    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        UserDetailsService userDetailsService = new UserDetailsService() {
            @Override
            public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
                User user = userRepository.findByUsername(username);
                if(user==null) throw new UsernameNotFoundException("用户不存在");
                return userRepository.findByUsername(username);
            }
        };
        auth.userDetailsService(userDetailsService);
    }
    //  /admin/hello
    //  /admin/**

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
               //         .withObjectPostProcessor()
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {

                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                        object.setSecurityMetadataSource(new CustomFilterInvocationSecurityMetadataSource(permissionRepository));
                        object.setAccessDecisionManager(new AccessDecisionManager());
                        return object;
                    }
                })

                .anyRequest().authenticated()
                  .and()
                .formLogin()
                .loginProcessingUrl("/login").permitAll()
//                .and()
//
//                .authorizeRequests()
//                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
//
//                    @Override
//                    public <O extends FilterSecurityInterceptor> O postProcess(O object) {
//                        object.setSecurityMetadataSource(new CustomFilterInvocationSecurityMetadataSource(urlRepository));
//                        object.setAccessDecisionManager(new AccessDecisionManager());
//                        return object;
//                    }
//                })

                .and()
                .csrf().disable()
                ;
    }
}
